/**
 */
package com.android.booksreader;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.view.WindowManager;

/**
 * @author revees
 * 
 */
public class MainActivity extends Activity {

	private int winWidth, winHeight; // 屏幕的宽和高
	private Bitmap currentBitmap, nextBitmap; // 存储当前的和下一张的绘图
	private Canvas currentCanvas, nextCanvas; // 定义画布
	private PageView pageView;
	private WelcomeView welcomeView;
	private Handler myHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 1) {
				if (welcomeView != null) {
					// 初始化pageview
					initPageView();
					welcomeView = null;
					setContentView(pageView);
				}
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 取消标题栏
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		// 设置程序为全屏
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		initWelcomeView();
		// 设置初始画面为欢迎画面
		setContentView(welcomeView);
	}

	/**
	 * 初始化欢迎界面
	 */
	private void initWelcomeView() {
		welcomeView = new WelcomeView(this, myHandler);
	}

	protected void initPageView() {
		// 获取屏幕的宽和高
		winWidth = getWindowManager().getDefaultDisplay().getWidth();
		winHeight = getWindowManager().getDefaultDisplay().getHeight();
		pageView = new PageView(this);
		// 创建两张bitmap
		currentBitmap = Bitmap.createBitmap(winWidth, winHeight, Bitmap.Config.ARGB_8888);
		nextBitmap = Bitmap.createBitmap(winWidth, winHeight, Bitmap.Config.ARGB_8888);
		// 用bitmap图像来填充canvas
		currentCanvas = new Canvas(currentBitmap);
		nextCanvas = new Canvas(nextBitmap);

		PageViewFactory pvFactory = new PageViewFactory(winWidth, winHeight);
		// 设置pageview的背景
		pvFactory.setBg(BitmapFactory.decodeResource(getResources(), R.drawable.bg));
		// 第一次启动时需要读取sharedpreference，从上一次退出的位置开始显示
		// 打开图书，移动到指定的位置
		// 准备当前页面的内容
		// 为pageView设置触摸时间监听
	}

}
